﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LibSolPedI;
using WebLibSolPedBE;
using SAP.Middleware.Connector;

namespace LibSolPedDALC
{
    public class LiberaPedidoCompraDALC : ILiberaPedidoCompra
    {
        private LiberaPedidoCompraDALC() { }
        private static volatile LiberaPedidoCompraDALC _instance = null;

        public static LiberaPedidoCompraDALC Instance()
        {
            if (_instance == null)
            {
                lock (typeof(LiberaPedidoCompraDALC))
                {
                    if (_instance == null)
                    {
                        _instance = new LiberaPedidoCompraDALC();
                    }
                }
            }
            return _instance;
        }

        public List<WebLibSolPedBE.PedidoCompraCabBE> getLstPedidoCompraCab()
        {
            List<PedidoCompraCabBE> lst = new List<PedidoCompraCabBE>();

            IRfcFunction function = null;
            RfcDestination dest = null;
            DestConfig dc = new DestConfig();
            try
            {
                function = DALC.rfcRep.CreateFunction("ZMOVIL_MM_CONSULTA_PEDIDO_CAB");

                function.Invoke(DALC.rfcDest);
                IRfcTable T_RETORNO = function.GetTable("T_PEDIDO_CAB");
                foreach (IRfcStructure item in T_RETORNO)
                {
                    PedidoCompraCabBE it = new PedidoCompraCabBE();
                    it.PO_NUMBER = item["PO_NUMBER"].GetString();
                    it.DOC_DATE = item["DOC_DATE"].GetString();
                    it.VENDOR = item["VENDOR"].GetString();
                    it.TARGET_VAL = item["TARGET_VAL"].GetString();
                    it.CREATED_BY = item["CREATED_BY"].GetString();
                    it.MONEDA = item["MONEDA"].GetString();
                    it.FRGGR = item["FRGGR"].GetString();
                    it.VALOR1 = item["VALOR1"].GetString();
                    it.VALOR2 = item["VALOR2"].GetString();
                    it.VALOR3 = item["VALOR3"].GetString();
                    it.VALOR4 = item["VALOR4"].GetString();
                    it.VALOR5 = item["VALOR5"].GetString();
                    lst.Add(it);
                }
            }
            catch (Exception e)
            {

            }

            return lst;
        }

        public List<WebLibSolPedBE.PedidoCompraDetBE> getLstPedidoCompraDet(string IP_NUMBER, string IP_FRGGR)
        {
            List<PedidoCompraDetBE> lst = new List<PedidoCompraDetBE>();

            IRfcFunction function = null;
            RfcDestination dest = null;
            DestConfig dc = new DestConfig();
            try
            {
                function = DALC.rfcRep.CreateFunction("ZMOVIL_MM_CONSULTA_PEDIDO_DET");
                function.SetValue("IP_NUMBER", IP_NUMBER);
                function.SetValue("IP_FRGGR", IP_FRGGR);
                function.Invoke(DALC.rfcDest);
                IRfcTable T_RETORNO = function.GetTable("T_PEDIDO_DET");
                foreach (IRfcStructure item in T_RETORNO)
                {
                    PedidoCompraDetBE it = new PedidoCompraDetBE();
                    it.PO_NUMBER = item["PO_NUMBER"].GetString();
                    it.PO_ITEM = item["PO_ITEM"].GetString();
                    it.SHORT_TEXT = item["SHORT_TEXT"].GetString();
                    it.UNIT = item["UNIT"].GetString();
                    it.DISP_QUAN = item["DISP_QUAN"].GetString();
                    it.NET_PRICE = item["NET_PRICE"].GetString();
                    it.PLANT = item["PLANT"].GetString();
                    it.VALOR1 = item["VALOR1"].GetString();
                    it.VALOR2 = item["VALOR2"].GetString();
                    it.VALOR3 = item["VALOR3"].GetString();
                    it.VALOR4 = item["VALOR4"].GetString();
                    it.VALOR5 = item["VALOR5"].GetString();
                    lst.Add(it);
                }
            }
            catch (Exception e)
            {

            }

            return lst;
        }

        public string setLiberaPedidoCompra(string IP_NROREP)
        {
            string ret = "";
            IRfcFunction function = null;
            RfcDestination dest = null;
            DestConfig dc = new DestConfig();
            try
            {
                function = DALC.rfcRep.CreateFunction("ZMOVIL_MM_LIBERACION_PED_COMPR");
                function.SetValue("IP_NROREP", IP_NROREP);
                function.Invoke(DALC.rfcDest);
                IRfcTable T_RETORNO = function.GetTable("T_RETORNO");
                foreach (IRfcStructure item in T_RETORNO)
                {
                    ret += item["MESSAGE"].GetString() + ". ";
                }
            }
            catch (Exception e)
            {
                ret = e.Message;
            }

            return ret;
        }
    }
}
